package com.xt.controller;

import com.xt.bean.Department;
import com.xt.bean.PlatformResult;
import com.xt.service.DepartmentService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * Created by XT on 2018/5/4.
 */
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    DepartmentService ds;

    @ApiOperation(value = "新增/修改信息", notes = "新增/修改信息")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public PlatformResult insert(@ApiParam(required = true, name = "department", value = "Department实体的字段名") @RequestBody Department d) {
        //检查参数
        ds.saveOrUpdateOne(d);
        return PlatformResult.ok();
    }

    @ApiOperation(value = "删除信息", notes = "删除信息")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public PlatformResult delete(@ApiParam(required = true, name = "department", value = "User实体的字段名") @RequestBody Department d) {
        //检查参数
        ds.deleteOne(d.getId());
        return PlatformResult.ok();
    }

    @ApiOperation(value = "列表", notes = "查询列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public PlatformResult list(@ApiParam(required = true, name = "count", value = "每页条数") @RequestParam int count,
                               @ApiParam(required = true, name = "pageNo", value = "当前页") @RequestParam int pageNo,
                               @ApiParam(required = true, name = "sortDirection", value = "排序方式（asc/desc）") @RequestParam String sortDirection,
                               @ApiParam(required = true, name = "sortField", value = "排序字段") @RequestParam String[] sortField) {

        Page<Department> page = ds.getListByPage(count, pageNo, sortDirection, sortField);
        return PlatformResult.ok(page);
    }

    @ApiOperation(value = "根据id查", notes = "根据id查")
    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    public PlatformResult getById(@ApiParam(required = true, name = "id", value = "id") @RequestParam String id) {
        Department a = ds.getById(id);
        return PlatformResult.ok(a);
    }

    @ApiOperation(value = "根据name模糊查询列表", notes = "根据name模糊查询列表")
    @RequestMapping(value = "/list/name", method = RequestMethod.GET)
    public PlatformResult listByName(@ApiParam(required = true, name = "name", value = "搜索关键字") @RequestParam String name,
                                     @ApiParam(required = true, name = "count", value = "每页条数") @RequestParam int count,
                                     @ApiParam(required = true, name = "pageNo", value = "当前页") @RequestParam int pageNo,
                                     @ApiParam(required = true, name = "sortDirection", value = "排序方式（asc/desc）") @RequestParam String sortDirection,
                                     @ApiParam(required = true, name = "sortField", value = "排序字段") @RequestParam String[] sortField) {

        Page<Department> page = ds.getPageByName(name, count, pageNo, sortDirection, sortField);
        return PlatformResult.ok(page);
    }

}
